package com.qianfeng.tiantian_banjia.fragment;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.qianfeng.tiantian_banjia.AiguangInfoActivity;
import com.qianfeng.tiantian_banjia.R;
import com.qianfeng.tiantian_banjia.adapter.MyGridViewAdapter;
import com.qianfeng.tiantian_banjia.adapter.MyListviewAdapter;
import com.qianfeng.tiantian_banjia.bean.AllGoods;
import com.qianfeng.tiantian_banjia.bean.Goods;
import com.qianfeng.tiantian_banjia.callback.CallBack;
import com.qianfeng.tiantian_banjia.task.StringAsyncTask;
import com.qianfeng.tiantian_banjia.utils.AppContasnts;
import com.qianfeng.tiantian_banjia.utils.AsyncImageLoader;
import com.qianfeng.tiantian_banjia.view.XGridView;
import com.qianfeng.tiantian_banjia.view.XListView;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

public class AiGuangFragment extends Fragment {
	private XGridView xGridView;
	private StringAsyncTask stringAsyncTask, stringAsyncTask2;
	private List<AllGoods> listGood = new ArrayList<AllGoods>();
	private MyGridViewAdapter gridViewAdapter;
	private AsyncImageLoader loader;
	private String url;
	private String title;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.from(getActivity()).inflate(
				R.layout.aiguang_fragment, container, false);
		// TODO Auto-generated method stub
	
		loader = new AsyncImageLoader(getActivity());
		init(view);
		getData(AppContasnts.getStringAiguang());
		return view;
	}

	public void getData(String path) {
		// TODO Auto-generated method stub
		stringAsyncTask = new StringAsyncTask(new CallBack() {

			@Override
			public void getStringData(String data) {
				List<AllGoods> list = new ArrayList<AllGoods>();
				// TODO Auto-generated method stub
				if (!TextUtils.isEmpty(data)) {

					try {
						JSONObject jsonResult = new JSONObject(data);
						JSONArray jsonArray = jsonResult.optJSONArray("data");
						Log.i("123", "Jsoadrr" + jsonArray.toString());
						for (int i = 0; i < jsonArray.length(); i++) {
							JSONObject goodsJson = jsonArray.optJSONObject(i);

							AllGoods good = new AllGoods();
							good.paseGood(goodsJson);
							url = good.getMobile_url();
							title = good.getTitle();
							list.add(good);

						}
						listGood.addAll(list);
						Log.i("123", "listsize" + listGood.size());
						gridViewAdapter.notifyDataSetChanged();
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					Log.i("123", listGood.size() + "");

				}
			}
		});

		stringAsyncTask.execute(path);
	}

	public void init(View view) {
		xGridView = (XGridView) view.findViewById(R.id.xgridView);
		gridViewAdapter = new MyGridViewAdapter(getActivity(), listGood, loader);
		xGridView.setNumColumns(2);
		xGridView.setAdapter(gridViewAdapter);
		xGridView.setPullLoadEnable(true);
		xGridView.setPullRefreshEnable(true);
		xGridView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method st;ub
				Intent intent = new Intent();
				intent.setClass(getActivity(), AiguangInfoActivity.class);
				intent.putExtra("title", title);
				intent.putExtra("url", url);
				startActivity(intent);

			}
		});
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		stringAsyncTask.cancel(true);
	}
}
